// 数据初始化，更新同步
import upload from "./upload.js";

import {
	getDictTypeData
} from "@/api/system/dict/data.js";
import {
	getDataByKpi
} from "@/api/sggl/craft.js";
import { getImages } from "@/api/sggl/record";
import {
			parseTime
		} from "@/utils/utils.js";
import {
		listDevice,deviceMatterList
	} from "@/api/sggl/device.js";		
	import { 
		getInstallDeviceList,
		getDeviceMatterList,
		getInstallCheckDeviceList,
		saveDeviceAzData,
		saveMatterReformData,
		saveDeviceAzCheckData,
		getMatterCheckList,
		saveMatterCheckData,
		saveAndDelMatterDeviceData
	} from "@/api/sggl/dataSync.js";
	import {treeselectProject} from "@/api/sggl/project.js"
	import {listQrCode} from "@/api/sggl/qrCode.js";
	import {listMatter} from "@/api/sggl/matter.js";

// 初始化kpi数据
export async function initCraftKpi() {
	var res = await getDataByKpi();
	console.log('初始化kpi数据', res)
	if (res.data.code == 200) {
		let craftKpi = {};
		let craftKpiList = res.data.data;
		if (craftKpiList) {
			craftKpiList.filter(e => {
				craftKpi['' + e.id] = e.kpis;
			})
		}
		uni.setStorageSync('craftKpi', craftKpi)
		return true;
	}else{
		return false;
	}
}

// 初始化字典数据
export async function initDictData() {
	var res = await getDictTypeData();
	console.log('初始化字典数据', res)
	// 初始化字典
	if (res.data.code == 200) {
		var dictData = {};
		var data = res.data.data;
		data.filter(d => {
			dictData['' + d.dictType] = d.dataList;
		})
		uni.setStorageSync('dictData', dictData)
		return true;
	}else{
		return false;
	}
}


// 初始化今日施工
export async function initJrsg() {
	console.log('初始化今日施工', res)
	
	var res = await getImages({
		createTimeStr: parseTime(new Date(),'{y}-{m}-{d}')
	});
	
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('jrsgData', data)
		return true;
	}else{
		return false;
	}
}

// 初始化安装设备列表
export async function initInstallDeviceList(){
	console.log('初始化安装设备列表')
	var res = await getInstallDeviceList();
	
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('installDeviceListData', data);
		return true;
	}else{
		return false;
	}
}

// 初始化设备问题数量列表
export async function  initDeviceMatterList(){
	console.log('初始化设备问题数量列表')
	var res = await getDeviceMatterList();
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('deviceMatterListData', data);
		return true;
	}else{
		console.log(res)
		return false;
	}
}

// 初始化安装审核设备列表
export async function initInstallCheckDeviceList(){
	console.log('初始化安装审核设备列表');
	var res = await getInstallCheckDeviceList();
	
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('installCheckDeviceListData', data);
		return true;
	}else{
		return false;
	}
}

// 问题审核列表
export async function initMatterCheckList(){
	console.log('初始化问题审核列表');
	var res = await getMatterCheckList();
	
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('initMatterCheckListData', data);
		return true;
	}else{
		return false;
	}
}


// 上传设备安装数据
export async function deviceAzData(){
	var deviceAzData = uni.getStorageSync('deviceAzData');
	
	// 上传图片
	for(var k in deviceAzData){
		var arr = deviceAzData[k];
		var res = await uploadImages(arr);
	}
	
	console.log('上传设备安装数据',deviceAzData);
	return await saveDeviceAzData({data:deviceAzData});
}

// 上传问题整改数据
export async function matterReformData(){
	var matterReformData = uni.getStorageSync('matterReformData');
	
	// 上传图片
	for(var k in matterReformData){
		var arr = matterReformData[k];
		var res = await uploadImages(arr);
	}
	
	console.log('上传问题整改数据',matterReformData);
	return await saveMatterReformData({data: matterReformData});
}

// 保存设备安装审核数据
export async function deviceAzCheckData(){
	var deviceAzCheckData = uni.getStorageSync('deviceAzCheckData');
	
	// 上传图片
	for(var k in deviceAzCheckData){
		var arr = deviceAzCheckData[k];
		var res = await uploadImages(arr);
	}
	
	console.log('上传问题整改数据',deviceAzCheckData);
	return await saveDeviceAzCheckData({data: deviceAzCheckData});
}

// 保存设备整改审核数据
export async function matterCheckData(){
	var matterCheckData = uni.getStorageSync('matterCheckData');
	
	// 上传图片
	var res = await uploadImages(matterCheckData);
	
	console.log('上传问题整改数据',matterCheckData);
	return await saveMatterCheckData({data: matterCheckData});
}



// 上传图片
async function uploadImages(arr){
	console.log(arr)
	for(let i=0;i<arr.length;i++){
		let e = arr[i];
		let imgPaths = [];
		var imgs = e.imgPath.split(',');
		if(imgs && imgs.length>0){
			
			for(let j=0;j<imgs.length;j++){
				let img = imgs[j];
				if(img && img.indexOf("//tmp")>-1){
					var res = await upload(img);
					console.log(res)
					if(res.data){
						var data = JSON.parse(res.data);
						imgPaths.push(data.fileName);
					}
				}
			}
		}
		
		e.imgPath = imgPaths.join(',');
	}
	return true;
}

// 初始化项目列表
export async function initTreeselectProject(){
	// console.log('初始化项目结构列表');
	var res = await treeselectProject();
	
	if (res.data.code == 200) {
		var data = res.data.data;
		uni.setStorageSync('initTreeselectProject', data);
		return true;
	}else{
		return false;
	}
}

// 设备二维码
export async function initListQrCode(){
	var res = await listQrCode({status: -1});
	
	if (res.data.code == 200) {
		var data = res.data.rows;
		uni.setStorageSync('initListQrCode', data);
		return true;
	}else{
		return false;
	}
}

// 问题列表
export async function initMatterList(){
	var res = await listMatter();
	
	if (res.data.code == 200) {
		var data = res.data.rows;
		uni.setStorageSync('initMatterList', data);
		return true;
	}else{
		return false;
	}
}

// 设备列表
export async function initDeviceList(){
	var res = await listDevice();
	
	if (res.data.code == 200) {
		var data = res.data.rows;
		uni.setStorageSync('initDeviceList', data);
		return true;
	}else{
		return false;
	}
}

// 保存问题发现数据
export async function matterDeviceData(){
	var matterDeviceData = uni.getStorageSync('matterDeviceData');
	
	// 上传图片
	var res = await uploadImages(matterDeviceData);
	
	// for(var k in matterDeviceData){
	// 	var arr = matterDeviceData[k];
	// 	var res = await uploadImages(arr);
	// }
	
	console.log('上传问题发现数据',matterDeviceData);
	
	return await saveAndDelMatterDeviceData({data: matterDeviceData});
}
